package com.itheima.pinda.web.transportline;

import com.itheima.pinda.DTO.transportline.TransportLineTypeDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.transportline.PdTransportLineType;
import com.itheima.pinda.mapstruct.TransportDTO2Entity;
import com.itheima.pinda.service.PdTransportLineTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 线路类型模块
 *
 * @author llhuang10
 * @date 2021/10/6 20:17
 */
@Slf4j
@Validated
@RestController
@Api(tags = "线路类型模块")
@RequestMapping("/base/transportLine/type")
public class TransportLineTypeController {

    @Resource
    private TransportDTO2Entity dto2Entity;

    @Resource
    private PdTransportLineTypeService transportLineTypeService;

    /**
     * 添加线路类型
     *
     * @param dto 线路类型信息
     * @return 线路类型信息
     */
    @PostMapping
    @ApiOperation("添加线路类型")
    public TransportLineTypeDto saveTransportLineType(@Valid @RequestBody TransportLineTypeDto dto) {
        return transportLineTypeService.saveTransportLineType(dto);
    }

    /**
     * 根据id获取线路类型详情
     *
     * @param id 线路类型id
     * @return 线路类型详情
     */
    @GetMapping("/{id}")
    @ApiOperation("添加线路类型")
    public TransportLineTypeDto findById(@NotBlank(message = "线路类型id不能为空!") @PathVariable("id") String id) {
        PdTransportLineType pdTransportLineType = transportLineTypeService.getById(id);
        return dto2Entity.conver(pdTransportLineType);
    }

    /**
     * 获取线路类型分页数据
     *
     * @param page       页码
     * @param pageSize   页尺寸
     * @param typeNumber 类型编号
     * @param name       类型名称
     * @param agencyType 机构类型
     * @return 线路类型分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取线路类型分页数据")
    public PageResponse<TransportLineTypeDto> findByPage(@RequestParam(name = "page") Integer page,
                                                         @RequestParam(name = "pageSize") Integer pageSize,
                                                         @RequestParam(name = "typeNumber", required = false) String typeNumber,
                                                         @RequestParam(name = "name", required = false) String name,
                                                         @RequestParam(name = "agencyType", required = false) Integer agencyType) {
        return transportLineTypeService.findByPage(page, pageSize, typeNumber, name, agencyType);
    }

    /**
     * 获取线路类型列表
     *
     * @param ids 线路类型id列表
     * @return 线路类型列表
     */
    @GetMapping
    @ApiOperation("获取线路类型列表")
    public List<TransportLineTypeDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids) {
        return transportLineTypeService.findAll(ids);
    }

    /**
     * 更新线路类型信息
     *
     * @param id  线路类型id
     * @param dto 线路类型信息
     * @return 线路类型信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新线路类型信息")
    public TransportLineTypeDto update(@NotBlank(message = "线路类型id不能为空!") @PathVariable("id") String id,
                                       @Valid @RequestBody TransportLineTypeDto dto) {
        dto.setId(id);
        PdTransportLineType pdTransportLineType = dto2Entity.conver(dto);
        transportLineTypeService.updateById(pdTransportLineType);
        return dto;
    }

    /**
     * 删除线路类型
     *
     * @param id 线路类型id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("删除线路类型")
    public Result disable(@NotBlank(message = "线路类型id不能为空!") @PathVariable("id") String id) {
        PdTransportLineType pdTransportLineType = new PdTransportLineType();
        pdTransportLineType.setId(Long.parseLong(id));
        pdTransportLineType.setStatus(Constant.DATA_DISABLE_STATUS.byteValue());
        boolean res = transportLineTypeService.updateById(pdTransportLineType);
        return res ? Result.ok() : Result.error("删除路线失败, 请联系管理员!");
    }
}
